<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\model\Article as ArticleModel;

/**
 * 首页接口
 */
class Article extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function getArticle()
    {
        $data = (new ArticleModel())->where('status','1')->where("type",2)->order("id desc")->limit(40)->select();
        $data = $this->formateDataBylang($data,['title','descript'],true);
        if(empty($data)){
            $this->error('no');
        }else{
            $this->success('ok',$data);
        }

    }

    public function getZhence()
    {
        $data = (new ArticleModel())->where("id",'>',1)->where('status','1')->where("type",1)->order("id asc")->select();
        $data = $this->formateDataBylang($data,['title','descript'],true);
        foreach ($data as &$item){
           $item['title'] = ucwords(strtolower($item['title']));
        }
        if(empty($data)){
            $this->error('no');
        }else{
            $this->success('ok',$data);
        }

    }
    public function getEvent()
    {
        $data = (new ArticleModel())->where('status','1')->where("type",3)->where('status',1)->order("id desc")->limit(40)->select();
        $data = $this->formateDataBylang($data,['title','descript'],true);
        foreach ($data as &$item){
            $item['create_time'] = mb_substr($item['create_time'],0,11);
        }
        if(empty($data)){
            $this->error('no');
        }else{
            $this->success('ok',$data);
        }

    }
    public function articleDetail(){
        $id = input('id',0);
        if(empty($id)){
            $this->success('ok',[]);
        }else{
            $data = (new ArticleModel())->where('id',$id)->find();
        }
        $data = $this->formateDataBylang($data,['title','descript'],false);
        $this->success('ok',$data);
    }
}
